<template>
  <div class="wrapper">
    <v-head></v-head>
    <v-sidebar></v-sidebar>
    <div class="content-box" :class="{'content-collapse':collapse}">
      <v-tags></v-tags>
      <div class="content">
        <transition name="move" mode="out-in">
          <keep-alive :include="tagsList">
            <router-view></router-view>
          </keep-alive>
        </transition>
      </div>
    </div>

  </div>
</template>

<script>
import vHead from "./Header.vue";
import vSidebar from "./Sidebar.vue";
import vTags from "./Tags.vue";
import bus from "./bus";
import api from "@/api/index";

export default {
  data() {
    return {
      name: "--",
      isLoading: false,
      subForm: {
        password: null,
        passwordSure: null,
      },
      tagsList: [],
      collapse: false,
    };
  },
  components: {
    vHead,
    vSidebar,
    vTags,
  },
  created() {
    bus.$on("collapse", (msg) => {
      this.collapse = msg;
    });

    // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
    bus.$on("tags", (msg) => {
      let arr = [];
      for (let i = 0, len = msg.length; i < len; i++) {
        msg[i].name && arr.push(msg[i].name);
      }
      this.tagsList = arr;
    });
  },
  methods: {
    closePsw() {
      this.subForm.password = "";
      this.subForm.passwordSure = "";
    },
    sureEdit() {
      if (!this.subForm.password) return this.$message.error("请输入新密码");
      if (!this.subForm.passwordSure)
        return this.$message.error("请再次输入新密码");
      if (this.subForm.password !== this.subForm.passwordSure)
        return this.$message.error("密码输入不一致");
      console.log(this.subForm.password);
      let Base64 = require("js-base64").Base64;
      let send = {
        id: JSON.parse(sessionStorage.getItem("adminUserInfo")).id,
        password: Base64.encode(this.subForm.password),
      };
      this.isLoading = true;
      api
        .updateUser(send)
        .then(
          (res) => {
            if (res.code == 200) {
              this.isLoading = false;
              this.$message.success("成功");
              //跳转到登录页面
              sessionStorage.removeItem("adminUserInfo");
              sessionStorage.removeItem("token");
              sessionStorage.removeItem("resources");
              sessionStorage.removeItem("userPwdIsExpire");
              this.$router.push("/login");
            }
          },
          (error) => {
            this.isLoading = false;
          }
        )
        .catch(() => {
          this.isLoading = false;
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
  },
  computed: {
    username() {
      let username = JSON.parse(sessionStorage.getItem("adminUserInfo"))
        .realName;
      return username ? username : this.name;
    },
  },
};
</script>
